class Solution
{
public:
    string compressString(string S)
    {
        string compressed;
        int count = 0;
        char last = S[0];
        for (char c : S)
        {
            if (c == last)
            {
                ++count;
            }
            else
            {
                compressed.push_back(last);
                compressed += to_string(count);
                count = 1;
                last = c;
            }
        }
        compressed.push_back(last);
        compressed += to_string(count);
        if (compressed.size() >= S.size())
        {
            return S;
        }
        return compressed;
    }
};